home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / isc-src / util / netupds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  3.4 KB  |  173 lines

  1. /* @(#) $Header: netupds.c,v 1.4 90/03/12 13:21:31 deyke Exp $ */
  2.  
  3. /* Net Update Server */
  4.  
  5. #include <sys/types.h>
  6.  
  7. #include <ctype.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <sys/stat.h>
  11.  
  12. extern void exit();
  13. extern void perror();
  14.  
  15. /*---------------------------------------------------------------------------*/
  16.  
  17. static void pexit(s)
  18. char  *s;
  19. {
  20.   perror(s);
  21.   exit(1);
  22. }
  23.  
  24. /*---------------------------------------------------------------------------*/
  25.  
  26. static void doread(fd, buf, cnt)
  27. int  fd;
  28. char  *buf;
  29. unsigned int  cnt;
  30. {
  31.  
  32.   char  *p = buf;
  33.   int  n;
  34.  
  35.   while (cnt) {
  36.     n = read(fd, p, cnt);
  37.     if (n < 0) pexit("read()");
  38.     if (!n) {
  39.       printf("read(): End of file\n");
  40.       exit(1);
  41.     }
  42.     p += n;
  43.     cnt -= n;
  44.   }
  45. }
  46.  
  47. /*---------------------------------------------------------------------------*/
  48.  
  49. static void dowrite(fd, buf, cnt)
  50. int  fd;
  51. char  *buf;
  52. unsigned int  cnt;
  53. {
  54.  
  55.   char  *p = buf;
  56.   int  n;
  57.  
  58.   while (cnt) {
  59.     n = write(fd, p, cnt);
  60.     if (n <= 0) pexit("write()");
  61.     p += n;
  62.     cnt -= n;
  63.   }
  64. }
  65.  
  66. /*---------------------------------------------------------------------------*/
  67.  
  68. int  main()
  69. {
  70.  
  71.   char  buf[1024];
  72.   char  client[1024];
  73.   char  filename[1024];
  74.   int  fdfile;
  75.   int  fdpipe[2];
  76.   int  fdsocket;
  77.   int  filesize;
  78.   int  i;
  79.   struct stat statbuf;
  80.  
  81.   alarm(6 * 3600);
  82.  
  83.   umask(022);
  84.   putenv("HOME=/users/root");
  85.   putenv("LOGNAME=root");
  86.   putenv("PATH=/bin:/usr/bin:/usr/local/bin:/usr/contrib/bin");
  87.   putenv("SHELL=/bin/sh");
  88.   putenv("TZ=MEZ-1MESZ");
  89.  
  90.   if ((fdsocket = dup(0)) < 3) exit(1);
  91.   if (pipe(fdpipe)) exit(1);
  92.   switch (fork()) {
  93.   case -1:
  94.     exit(1);
  95.   case 0:
  96.     for (i = 0; i < _NFILE; i++)
  97.       if (i != fdpipe[0]) close(i);
  98.     dup(fdpipe[0]);
  99.     open("/dev/null", O_RDWR, 0666);
  100.     open("/dev/null", O_RDWR, 0666);
  101.     close(fdpipe[0]);
  102.     execl("/usr/bin/mailx", "mailx", "-s", "netupds log", "root", (char *) 0);
  103.     exit(1);
  104.   default:
  105.     for (i = 0; i < _NFILE; i++)
  106.       if (i != fdpipe[1] && i != fdsocket) close(i);
  107.     open("/dev/null", O_RDWR, 0666);
  108.     dup(fdpipe[1]);
  109.     dup(fdpipe[1]);
  110.     close(fdpipe[1]);
  111.   }
  112.  
  113.   for (i = 0; ; i++) {
  114.     if (i >= sizeof(client)) {
  115.       printf("Client name too long\n");
  116.       exit(1);
  117.     }
  118.     doread(fdsocket, client + i, 1);
  119.     if (!client[i]) break;
  120.     if (!isalnum(client[i] & 0xff) && client[i] != '-') {
  121.       printf("Bad char in client name\n");
  122.       exit(1);
  123.     }
  124.   }
  125.   if (!*client) {
  126.     printf("Null client name\n");
  127.     exit(1);
  128.   }
  129.  
  130.   printf("Client = %s\n", client);
  131.   fflush(stdout);
  132.  
  133.   sprintf(buf, "/users/funk/dk5sg/tcp.%s", client);
  134.   if (chdir(buf)) pexit(buf);
  135.  
  136.   tmpnam(filename);
  137.   sprintf(buf, "/users/funk/dk5sg/tcp/util/genupd %s | compress > %s", client, filename);
  138.   system(buf);
  139.  
  140.   if (stat(filename, &statbuf)) pexit(filename);
  141.   filesize = statbuf.st_size;
  142.  
  143.   printf("File size = %i\n", filesize);
  144.   fflush(stdout);
  145.  
  146.   dowrite(fdsocket, (char *) & filesize, 4);
  147.  
  148.   fdfile = open(filename, O_RDONLY, 0600);
  149.   if (fdfile < 0) pexit(filename);
  150.   while (filesize > 0) {
  151.     i = filesize < sizeof(buf) ? filesize : sizeof(buf);
  152.     doread(fdfile, buf, (unsigned) i);
  153.     dowrite(fdsocket, buf, (unsigned) i);
  154.     filesize -= i;
  155.   }
  156.   if (close(fdfile)) pexit("close()");
  157.  
  158.   doread(fdsocket, (char *) & i, 4);
  159.  
  160.   printf("Response = %i\n", i);
  161.   fflush(stdout);
  162.  
  163.   if (!i) {
  164.     sprintf(buf, "uncompress < %s | sh", filename);
  165.     system(buf);
  166.   }
  167.  
  168.   if (unlink(filename)) pexit(filename);
  169.  
  170.   return 0;
  171. }
  172.  
  173.